package com.globallogic.training.javase.university.subject;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.HashSet;

import org.apache.commons.lang.StringUtils;

import com.globallogic.training.javase.university.member.Group;
import com.globallogic.training.javase.university.member.Teacher;

/**
 * This class represents the subjects that groups should study in the
 * university. This class also includes the activities related to this Subject.
 * 
 * @author Ivan Moisiienko
 * @version 0.2 2013/05/13
 */
public class Subject implements Comparable<Subject> {

	private String subjectName;
	private Set<Activity> activities = new HashSet<Activity>();
	private List<Teacher> teachers = new ArrayList<Teacher>();

	/**
	 * @param subjectName
	 *            Subject name cannot be null or blank
	 */
	public Subject(String subjectName) {

		if (subjectName == null) {
			throw new IllegalArgumentException("Subject Name is null");
		}

		if (StringUtils.isBlank(subjectName)) {
			throw new IllegalArgumentException("Subject Name is blank");
		}

		this.subjectName = subjectName;
	}

	public String getName() {
		return subjectName;
	}

	public Activity createActivity(ActivityType activityType, int numberInPlan,
			Group group) {
		Activity activity = null;

		switch (activityType) {
		case LECTURE:
			activity = new Lecture(numberInPlan, getDefaultTeacher(), group,
					this);
			break;
		case SEMINAR:
			activity = new Seminar(numberInPlan, getDefaultTeacher(), group,
					this);
			break;
		case EXAM:
			activity = new Examination(getDefaultTeacher(), group, this);
			break;
		}

		activities.add(activity);

		return activity;
	}

	public boolean removeActivity(Activity a) {
		return activities.remove(a);
	}

	public Set<Activity> getActivitiesByGroup(Group g) {
		Set<Activity> tempActivities = new HashSet<Activity>();

		for (Activity a : activities) {
			if (a.getGroup().getGroupId().equals(g.getGroupId())) {
				tempActivities.add(a);
			}
		}

		return tempActivities;
	}

	public Set<Activity> getActivitiesByTeacher(Teacher t) {
		Set<Activity> tempActivities = new HashSet<Activity>();

		for (Activity a : activities) {
			if (a.getTeacher().getFullName().equals(t.getFullName())) {
				tempActivities.add(a);
			}
		}

		return tempActivities;
	}

	public boolean addTeacher(Teacher tch) {
		if (tch == null) {
			return false;
		}

		return teachers.add(tch);
	}

	public boolean removeTeacher(Teacher tch) {
		return teachers.remove(tch);
	}

	public List<Teacher> getTeachers() {
		return teachers;
	}

	public Teacher getDefaultTeacher() {
		int size = teachers.size();

		if (size == 0) {
			throw new IllegalStateException(
					"There should be at least one teacher");
		}

		return teachers.get(0);
	}

	public String toString() {
		return subjectName;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + subjectName.hashCode();
		return result;
	}

	@Override
	public boolean equals(Object otherObject) {
		if (this == otherObject) {
			return true;
		}
		if (otherObject == null) {
			return false;
		}
		if (getClass() != otherObject.getClass()) {
			return false;
		}

		Subject other = (Subject) otherObject;
		if (subjectName.equals(other.subjectName)) {
			return true;
		}
		return false;
	}

	@Override
	public int compareTo(Subject otherSubject) {
		String name = this.subjectName;
		String otherName = otherSubject.subjectName;
		int compare = name.compareTo(otherName);

		if (compare > 0) {
			return 1;
		}
		if (compare < 0) {
			return -1;
		}
		return 0;
	}
}
